﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel;
using System.Text.RegularExpressions;

namespace DealKhuyenMaiV2.com.Models
{
    [MetadataType(typeof(Email_Validation))]
    public partial class Email
    {

        public static Email CreateNewEmail(String IdEmail)
        {
            string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                                @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                                @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

            Regex re = new Regex(strRegex);

            if (re.IsMatch(IdEmail))
            {
                Email email = new Email();
                email.Email1 = IdEmail;
                return email;
            }
            else
            {
                return null;
            } 
        }

       
    }

    public class EmailAttribute : RegularExpressionAttribute
    {
        public EmailAttribute() : base("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$") { }
    }

    public class Email_Validation
    {
        [DisplayName("Email")]
        [Required(ErrorMessage = "Email bắt buộc")]
        [Email(ErrorMessage = "Not a valid email")]
        public string Email1 { get; set; }

        [DisplayName("Ngày thêm vào")]
        public DateTime DateRegister { get; set; }

    }
}